package com.weina.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weina.entity.Dept;
import com.weina.service.DeptService;
import com.weina.utils.DataGridViewResult;
import com.weina.utils.SystemConstant;
import com.weina.vo.DeptVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/dept")
public class DeptController {
    @Resource
    private DeptService deptService;

    @RequestMapping("/list")
    public DataGridViewResult list(DeptVo deptVo){
        //设置分页信息
        PageHelper.startPage(deptVo.getPage(), deptVo.getLimit());
        List<Dept> deptList = deptService.findDeptListByPage(deptVo);
        PageInfo<Dept> deptPageInfo = new PageInfo<>(deptList);
        //返回数据
        return new DataGridViewResult(deptPageInfo.getTotal(),deptPageInfo.getList());
    }

    @RequestMapping(value = "/addDept",method = RequestMethod.POST)
    public String addDept(Dept dept){
        Map<String,Object> map = new HashMap<String,Object>();
        if(deptService.insertDept(dept)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);//失败
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping(value = "/updateDept",method = RequestMethod.PUT)
    public String updateDept(Dept dept){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用修改部门的方法
        if(deptService.updateDept(dept)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/checkDeptHasEmployee")
    public String checkDeptHasEmployee(Integer deptId){
        Map<String,Object> map = new HashMap<String,Object>();
        if(deptService.getEmployeeCountByDeptId(deptId)>0){
            //部门下面有员工
            map.put(SystemConstant.EXIST,true);
            map.put(SystemConstant.MESSAGE,"改部门下存在员工信息，无法删除");
        }else{
            //可以删除
            map.put(SystemConstant.EXIST,false);
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping(value = "/deleteById/{id}",method = RequestMethod.DELETE)
    public String deleteById(@PathVariable("id") Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if(deptService.deleteDept(id)>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/deptList")
    public String deptList(){
        return JSON.toJSONString(deptService.findDeptList());
    }
}
